/*
 * 纯框架层面的关于UI管理部分的实现
 * @author: huangww
 * @Date: 2021-02-22 14:40:58
 * @LastEditTime: 2021-03-01 13:55:53
 * @FilePath: \fmgr\src\core\mgr\UIMgr.ts
 */
namespace core {
    export class UIMgr {
        private static instance: UIMgr;
        private sceneLayer: eui.Group;
        private UILayer: eui.Group;
        private TOPLayer: eui.Group;
        /**UI堆栈,用于控制多层级情况下的显隐 */
        private layerStack: Array<Component>;

        private _scene: SceneComponent;//持有当前场景UI供内部使用

        private _logic: UILogic;

        public constructor() {
            this.layerStack = [];
            this._logic = new UILogic();
        }

        private init(stage) {
            this.sceneLayer = new eui.Group();
            this.sceneLayer.touchEnabled = false;

            this.UILayer = new eui.Group();
            this.UILayer.touchEnabled = false;

            this.TOPLayer = new eui.Group();
            this.TOPLayer.touchEnabled = false;
            // this.TOPLayer.touchThrough = false;

            stage.addChild(this.sceneLayer);
            stage.addChild(this.UILayer);
            stage.addChild(this.TOPLayer);
        }

        /**作为游戏初始化入口
         * 之后扩展龙骨管理，定时器管理，资源管理初始化
         */
        start(stage) {
            this.init(stage);
            TimerMgr.getInstance().start();
            // ResLoadManager.getInstance().start();
            this._logic.start();
        }

        openScene(scene: SceneComponent) {
            let children = this.sceneLayer.$children;
            for (let child of children) {
                let item = <Component>child;
                item.onClose();
            }
            let childrens = this.UILayer.$children;
            for (let child of childrens) {
                let item = <Component>child;
                item.onClose();
            }
            // ResLoadManager.getInstance().releaseRes();
            // DBManager.getInstance().releaseDragonData();
            UIMgr.getInstance().open(scene);

            this._logic.openScene();
        }

        changeScene(type: SCENETYPE, joinType?: number) {

        }

        getSceneType() {
            return this._scene.getSceneType();
        }

        /**
         * 作为所有非上层界面的统一开启入口
         * 当obj为场景对象时会将其他场景移除
         */
        open(obj: Component) {
            ResLoadMgr.getInstance().load(obj.res, this, () => {
                this.loadComplete(obj);
            });
        }

        private loadComplete(obj: Component) {
            switch (obj.UIType) {
                case COMPONENTTYPE.SCENE:
                    this.addScene(<SceneComponent>obj);
                    break;
                case COMPONENTTYPE.UI:
                case COMPONENTTYPE.WINDOW:
                    this.addUI(obj);
                    break;
                case COMPONENTTYPE.TOP:
                    this.addTop(obj);
                    break;
            }
        }

        /**所有界面关闭统一处理入口,一般情况下由界面的onClose执行 */
        close(obj: Component) {
            obj.clear();
            obj.release();
            switch (obj.UIType) {
                case COMPONENTTYPE.SCENE:
                    this.removeScene(<SceneComponent>obj);
                    break;
                case COMPONENTTYPE.UI:
                case COMPONENTTYPE.WINDOW:
                    this.removeUI(obj);
                    break;
                case COMPONENTTYPE.TOP:
                    this.removeTop(obj);
                    break;
            }
        }

        /**将要隐藏的界面推入栈中，在上层界面移除时弹出 */
        private push(obj: Component) {
            for (let i of this.layerStack) {
                if (i === obj) {
                    return;
                }
            }
            obj.active(false);
            this.layerStack.push(obj);
        }

        private addUI(obj: Component) {
            //执行场景UI的open动画
            if (obj.UIType === COMPONENTTYPE.UI) {
                if (!this._scene.hasOpenAction()) {
                    this._scene.openAction();
                }

                /**UI界面重叠时，将底层隐藏 */
                let children = this.UILayer.$children;
                for (let child of children) {
                    let item = <Component>child;
                    if (item.UIType === COMPONENTTYPE.UI) {
                        this.push(item);
                    }
                }
            }
            this.UILayer.addChild(obj);

        }

        private removeUI(obj: Component) {
            if (!obj.parent) return;
            this.UILayer.removeChild(obj);
            if (obj.UIType === COMPONENTTYPE.UI) {

                if (this.layerStack.length > 0) {
                    let ui = this.layerStack.pop();
                    if (ui) {
                        ui.active(true);
                    }
                } else {
                    //执行场景UI的关闭动画
                    if (this._scene.hasOpenAction()) {
                        this._scene.closeAction();
                    }
                }
            }
        }

        public isAddUI(name: string) {
            let uiArr = this.UILayer.$children;
            for (let i in uiArr) {
                if (uiArr[i]["name"] === name) {
                    return true;
                }
            }
            return false;
        }
        /**
         * 添加场景UI，场景UI在游戏中唯一
         */
        private addScene(obj: SceneComponent) {
            this.sceneLayer.addChild(obj);
            this._scene = obj;
        }
        private removeScene(obj: SceneComponent) {
            this.sceneLayer.removeChild(obj);
        }



        private addTop(obj: Component) {
            this.TOPLayer.addChild(obj);
        }
        private removeTop(obj: Component) {
            this.TOPLayer.removeChild(obj);
        }

        static getInstance() {
            if (!this.instance) {
                this.instance = new UIMgr();
            }
            return this.instance;
        }
    }

}